package iontec.behavior;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;


public class GoBDbAdapter {

    public static final String KEY_NAME = "name";
    public static final String KEY_BODY = "body";
    public static final String KEY_ROWID = "_id";
    public static final String KEY_NEGATIVE = "negative";
    public static final String KEY_VAL = "value";

    private static final String TAG = "GOBDbAdapter";
    private DatabaseHelper mDbHelper;
    private SQLiteDatabase mDb;

    
    // Database creation sql statement
     
    private static final String CREATE_TABLE_KIDS =
          "create table kids(_id integer primary key autoincrement, "
        + "name text not null, body text not null);";
    private static final String CREATE_TABLE_BEHAVOIRS = 
    	  "create table behaviors(_id integer primary key autoincrement, "
        + "name text not null, value integer not null);"; 
    private static final String CREATE_TABLE_STATS = 
    	  "create table stats(_id integer primary key autoincrement, "
        + "id_kid integer not null, id_behavior integer not null, date integer not null );"
        + "";

    private static final String DATABASE_NAME = "data";
    private static final String DATABASE_TABLE_K = "kids";
    private static final String DATABASE_TABLE_B = "behaviors";
    private static final String DATABASE_TABLE_S = "stats";
    private static final int DATABASE_VERSION = 4;

    private final Context mCtx;

    private static class DatabaseHelper extends SQLiteOpenHelper {

        DatabaseHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {

            db.execSQL(CREATE_TABLE_KIDS);
            db.execSQL(CREATE_TABLE_BEHAVOIRS);
            db.execSQL(CREATE_TABLE_STATS);
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
                    + newVersion + ", which will destroy all old data");
            db.execSQL("DROP TABLE IF EXISTS "+DATABASE_TABLE_K);
            db.execSQL("DROP TABLE IF EXISTS "+DATABASE_TABLE_B);
            db.execSQL("DROP TABLE IF EXISTS "+DATABASE_TABLE_S);
            onCreate(db);
        }
    }

    /**
     * Constructor - takes the context to allow the database to be
     * opened/created
     * 
     * @param ctx the Context within which to work
     */
    public GoBDbAdapter(Context ctx) {
        this.mCtx = ctx;
    }


    public GoBDbAdapter open() throws SQLException {
        mDbHelper = new DatabaseHelper(mCtx);
        mDb = mDbHelper.getWritableDatabase();
        return this;
    }

    public void close() {
        mDbHelper.close();
    }

    //  Kids methods section 
    
    public long createKid(String name, String body) {
        ContentValues initialValues = new ContentValues();
        initialValues.put(KEY_NAME, name);
        initialValues.put(KEY_BODY, body);

        return mDb.insert(DATABASE_TABLE_K, null, initialValues);
    }


    public boolean deleteKid(long rowId) {

        return mDb.delete(DATABASE_TABLE_K, KEY_ROWID + "=" + rowId, null) > 0;
    }


    public Cursor fetchAllKids() {

        return mDb.query(DATABASE_TABLE_K, new String[] {KEY_ROWID, KEY_NAME,
                KEY_BODY}, null, null, null, null, null);
    }


    public Cursor fetchKid(long rowId) throws SQLException {

        Cursor mCursor =

            mDb.query(true, DATABASE_TABLE_K, new String[] {KEY_ROWID,
                    KEY_NAME, KEY_BODY}, KEY_ROWID + "=" + rowId, null,
                    null, null, null, null);
        if (mCursor != null) {
            mCursor.moveToFirst();
        }
        return mCursor;

    }


    public boolean updateKid(long rowId, String name, String body) {
        ContentValues args = new ContentValues();
        args.put(KEY_NAME, name);
        args.put(KEY_BODY, body);

        return mDb.update(DATABASE_TABLE_K, args, KEY_ROWID + "=" + rowId, null) > 0;
    }
    
    // Behavior methods section
    
    public long createBehavior(String name, Integer value) {
        ContentValues initialValues = new ContentValues();
        initialValues.put(KEY_NAME, name);
        initialValues.put(KEY_VAL, value);
        

        return mDb.insert(DATABASE_TABLE_B, null, initialValues);
    }
    public boolean deleteBehavior(long rowId) {

        return mDb.delete(DATABASE_TABLE_B, KEY_ROWID + "=" + rowId, null) > 0;
    }
    public Cursor fetchAllBehaviors(int range) {
    	
    	String where = KEY_VAL; 
    	switch (range) {
		case -1: 
			where = where + " < 0";
			break;
		case 1: 
			where = where + " > 0";	
			break;
		default:
			where = "";
			break;
		}
    	
        return mDb.query(DATABASE_TABLE_B, new String[] {KEY_ROWID, KEY_NAME,
                KEY_VAL}, where, null, null, null, null);
    }

    public Cursor fetchBehavior(long rowId) throws SQLException {

        Cursor mCursor =
            mDb.query(true, DATABASE_TABLE_B, new String[] {KEY_ROWID,
            		 KEY_VAL}, KEY_ROWID + "=" + rowId, null,
                    null, null, null, null);
        if (mCursor != null) {
            mCursor.moveToFirst();
        }
        return mCursor;

    }

    public boolean updateBehavior(long rowId, String name, Integer value) {
        ContentValues args = new ContentValues();
        args.put(KEY_NAME, name);
        args.put(KEY_VAL, value);
        return mDb.update(DATABASE_TABLE_B, args, KEY_ROWID + "=" + rowId, null) > 0;
    }
    

} 